<?php
class Common_lib {
	protected $ci;
	public function __construct()
	{
		$this->ci =& get_instance();
	}
	
	function show_ok_result($resid) {
		$this->start_xml_output();
		echo "<$resid></$resid>";
		exit;
	}
	
	function show_error($message) {
		$this->start_xml_output();
		echo "<error><descr>$message</descr></error>";
		exit;
	}
	
	function start_xml_output()
	{
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Pragma: no-cache");
		header("Content-type: text/xml; charset=utf-8");
		/*echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";*/
		echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	}
	
	function start_html_output()
	{
		$charset = 'utf-8';
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Pragma: no-cache");
		header("Content-type: text/html" . (isset($charset) ? "; charset=" . $charset : ""));
	}
	
	function escape_with_cdata($text)
	{
		return "<![CDATA[" . str_replace("]]>", "]]>]]&gt;<![CDATA[", $text) . "]]>";
	}
	
	function get_useragent_version($userAgent)
	{
		$knownAgents = array("opera", "msie", "chrome", "safari", "firefox", "netscape", "mozilla");
		if (is_array($knownAgents)) {
			$userAgent = strtolower($userAgent);
			foreach ($knownAgents as $agent) {
				if (strstr($userAgent, $agent)) {
					if (preg_match("/" . $agent . "[\\s\/]?(\\d+(\\.\\d+(\\.\\d+(\\.\\d+)?)?)?)/", $userAgent, $matches)) {
						$ver = $matches[1];
						if ($agent == 'safari') {
							if (preg_match("/version\/(\\d+(\\.\\d+(\\.\\d+)?)?)/", $userAgent, $matches)) {
								$ver = $matches[1];
							} else {
								$ver = "1 or 2 (build " . $ver . ")";
							}
							if (preg_match("/mobile\/(\\d+(\\.\\d+(\\.\\d+)?)?)/", $userAgent, $matches)) {
								$userAgent = "iPhone " . $matches[1] . " ($agent $ver)";
								break;
							}
						}
	
						$userAgent = ucfirst($agent) . " " . $ver;
						break;
					}
				}
			}
		}
		return $userAgent;
	}
}
?>